package com.isoftstone.app.controller.card;


import com.isoftstone.app.entity.MyCardVO;
import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Tidings;
import com.isoftstone.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.text.DecimalFormat;
import java.util.*;


@Controller
public class CardController extends BaseController {

    protected Logger logger = Logger.getLogger(this.getClass());

    private static DecimalFormat df = new DecimalFormat("0.00");

    /**
     * 查询我的卡列表
     *
     * @param request
     * @param response
     * @return
     */

    @RequestMapping(value = "/app/admin/card/getMyCards", method = RequestMethod.POST)
    @ResponseBody
    public Object getMyCards(HttpServletRequest request, HttpServletResponse response) {


        Tidings<List<Map<String, String>>> tidings = new Tidings<List<Map<String, String>>>();

        String userid = (String) this.getSession().getAttribute("userid");

        try {

            List<Map<String, String>> cardMap = null;

            List<Map<String, String>> cardlist = new ArrayList<Map<String, String>>();

            /**
             * 增加 this.getSession().getAttribute(Const.CARD) == 0 的判断
             */

            cardMap = BocHttpUtil.getBocCard(request, "01");

            if (cardMap != null && cardMap.size() > 0) {
                for (Map<String, String> cards : cardMap) {
                    cardlist.add(cards);
                }
            }

            tidings.setT(cardlist);
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }

        return tidings;

    }

    /**
     * 首页查询余额
     * @param request
     * @return
     */
    @RequestMapping(value = "/app/admin/card/debitBalquery", method = RequestMethod.POST)
    @ResponseBody
    public Object debitbalquery(HttpServletRequest request) {

        Tidings<Map<String, Object>> tidings = new Tidings<Map<String, Object>>();
        try {
            Map<String, Object> reMap = new HashMap<String, Object>();

            double balance = 0;  //静资产
            double curBalance = 0;  //信用卡负债
            //获取所有的卡
            List<Map<String, String>> cardMap =  (List<Map<String, String>>)this.getSession().getAttribute(Const.CARD);

            logger.info("获取到所有的卡-----return："+ JsonUtil.beanToJson(cardMap));

            for (int i = 0; i < cardMap.size(); i++) {
                Map<String, String> mapCard = cardMap.get(i);
                if("03".equals(mapCard.get("bindType"))){   //信用卡负债
                    String cardSeq = mapCard.get("cardSeq");

                    logger.info("获取信用卡负责-----info："+ JsonUtil.beanToJson(cardSeq));

                    Map<String, Object> bal2 = BocHttpUtil.query_creditcard_balance(request, cardSeq);

                    logger.info("获取信用卡负责-----return："+ JsonUtil.beanToJson(bal2));

                    Map<String, Object> serviceResponse = (Map<String, Object>)bal2.get("serviceResponse");
                    String  responseCode = (String) serviceResponse.get("responseCode");
                    if("0000000".equals(responseCode)){
                        String str = (String)bal2.get("curBalance");
                        if(StringUtils.isNotBlank(str)){
                            curBalance = Double.parseDouble(str)/100;
                        }
                    }
                }else if("01".equals(mapCard.get("bindType"))){ //静资产
                    String cardSeq = mapCard.get("cardSeq");

                    logger.info("静资产-----info："+ JsonUtil.beanToJson(cardSeq));

                    Map<String, Object> bal = BocHttpUtil.debitMultiBlance(request, cardSeq);

                    logger.info("静资产-----return："+ JsonUtil.beanToJson(bal));

                    Map<String, Object> serviceResponse = (Map<String, Object>)bal.get("serviceResponse");
                    String  responseCode = (String) serviceResponse.get("responseCode");
                    if("0000000".equals(responseCode)){
                        Map<String, String> dto = (Map<String, String>)bal.get("cardServiceDTO");
                        String str = dto.get("balance");
                        if(StringUtils.isNotBlank(str)){
                            balance += Double.parseDouble(str)/ 100;
                        }
                    }
                }
            }
            reMap.put("balance",df.format(balance-curBalance));
            tidings.setT(reMap);
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            tidings.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return tidings;
    }

    @RequestMapping(value = "/app/admin/card/getmonth", method = RequestMethod.POST)
    @ResponseBody
    public Object getmonth() {
        Tidings<Map<String, Object>> tidings = new Tidings<Map<String, Object>>();

        Map<String, Object> shmap = new HashMap<String, Object>();

        String month0 = DateUtil.getLastMonth(0);
        String month1 = DateUtil.getLastMonth(-1);  // 上个月
        String month2 = DateUtil.getLastMonth(-2);
        String month3 = DateUtil.getLastMonth(-3);
        String month4 = DateUtil.getLastMonth(-4);
        String month5 = DateUtil.getLastMonth(-5);

        String month11[] = new String[]{month0,month1,month2,month3,month4,month5};
        shmap.put("month11",month11);

        tidings.setT(shmap);
        tidings.setStatus(Const.SUCCESS);


        return tidings;
    }

    /**
     * 根据卡序号查询余额
     *
     * @param request
     * @param cardSeq
     * @return
     */

    @RequestMapping(value = "/app/admin/card/debitBalqueryByCardSeq", method = RequestMethod.POST)
    @ResponseBody
    public Object debitBalqueryByCardSeq(HttpServletRequest request, String cardSeq) {
        Tidings<Map<String, Object>> tidings = new Tidings<Map<String, Object>>();

        try {

            Map<String, Object> reMap = new HashMap<String, Object>();
            List<MyCardVO> myCard = new ArrayList<MyCardVO>();


            double netAssets = 0;  //静资产
            double curBalance = 0;  //信用卡负债
            //获取所有的卡
            List<Map<String, String>> cardMap =  (List<Map<String, String>>)this.getSession().getAttribute(Const.CARD);

            logger.info("debitBalqueryByCardSeq-----获取到所有的卡-----return："+ JsonUtil.beanToJson(cardMap));
            reMap.put("responseCode","0000000");
            tidings.setStatus(Const.SUCCESS);

            for (int i = 0; i < cardMap.size(); i++) {
                Map<String, String> mapCard = cardMap.get(i);
                MyCardVO cardVO = new MyCardVO();

                /**
                 * 卡号不需要返回到前端
                 * 
                  */
                cardVO.setCardNo(mapCard.get("cardNo"));
                cardVO.setCardSeq(mapCard.get("cardSeq"));
                cardVO.setActName(mapCard.get("actName"));
                cardVO.setBindType(mapCard.get("bindType"));

                if("03".equals(mapCard.get("bindType"))){   //信用卡负债
                    String LIMITAMT = mapCard.get("cardSeq");
                    logger.info("debitBalqueryByCardSeq-----query_creditcard_balance-----info："+ JsonUtil.beanToJson(LIMITAMT));

                    Map<String, Object> bal2 = BocHttpUtil.query_creditcard_balance(request, LIMITAMT);
                    logger.info("debitBalqueryByCardSeq-----query_creditcard_balance-----return："+ JsonUtil.beanToJson(bal2));


                    Map<String, Object> serviceResponse = (Map<String, Object>)bal2.get("serviceResponse");
                    String  responseCode = (String) serviceResponse.get("responseCode");
                    if("0000000".equals(responseCode)){
                        String str = (String)bal2.get("curBalance");
                        if(StringUtils.isNotBlank(str)){
                             curBalance= Double.parseDouble(str)/100;
                            cardVO.setBalance(df.format(curBalance));

                        }
                    }
                }else if("01".equals(mapCard.get("bindType"))){ // 静资产
                    String LIMITAMT = mapCard.get("cardSeq");
                    String actName = mapCard.get("actName");

                    logger.info("debitBalqueryByCardSeq-----debitMultiBlance-----info："+ JsonUtil.beanToJson(LIMITAMT));
                    Map<String, Object> bal = BocHttpUtil.debitMultiBlance(request, LIMITAMT);
                    logger.info("debitBalqueryByCardSeq-----debitMultiBlance-----return："+ JsonUtil.beanToJson(bal));

                    Map<String, Object> serviceResponse = (Map<String, Object>)bal.get("serviceResponse");
                    String  responseCode = (String) serviceResponse.get("responseCode");
                    if("0000000".equals(responseCode)){
                        Map<String, String> dto = (Map<String, String>)bal.get("cardServiceDTO");
                        dto.put("cardSeq",cardSeq);
                        dto.put("actName",actName);
                        String str = dto.get("balance");
                        if(StringUtils.isNotBlank(str)){
                            netAssets += Double.parseDouble(str)/ 100;
                            cardVO.setBalance(df.format(Double.parseDouble(str)/ 100));
                        }
                    }else if("018440".equals(responseCode)){ // 卡关闭
                        cardVO.setBalance("0");
                  }else if("010188".equals(responseCode)){
                    cardVO.setBalance("0");

                }else{
                        reMap.put("responseCode","1111111");
                        tidings.setStatus(Const.ERROR);
                    }
                }
                myCard.add(cardVO);
            }
        
            reMap.put("curBalance",df.format(curBalance));
            reMap.put("netAssets",df.format(netAssets - curBalance));
            reMap.put("myCard",myCard);

            tidings.setT(reMap);


        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            tidings.setMsg(e.getMessage());
            e.printStackTrace();
        }
        logger.info("debitBalqueryByCardSeq-----debitMultiBlance-----tidings："+ JsonUtil.beanToJson(tidings));

        return tidings;
    }


    /**
     * 废弃 guoX
     * @param request
     * @return
     */
    @RequestMapping(value = "/app/admin/card/getTotalBal", method = RequestMethod.POST)
    @ResponseBody
    public Object getTotalBal(HttpServletRequest request) {


        Tidings<String> tidings = new Tidings<String>();


        List<Map<String, String>> cardMap = null;

        try {

            if (this.getSession().getAttribute(Const.CARD) == null) {
                cardMap = BocHttpUtil.getBocCard(request, "01");
                this.getSession().setAttribute(Const.CARD, cardMap);
            } else {
                cardMap = (List<Map<String, String>>) this.getSession().getAttribute(Const.CARD);
            }


            Double tatolBal = 0.0;

           if (cardMap!=null){
               for (Map<String, String> cardinfo : cardMap) {
                    String cardSeq =  (String) cardinfo.get("cardSeq");
                   logger.info("总资产计算,cardSeq="+cardSeq);
                   Map<String, Object> bal = BocHttpUtil.debitMultiBlance(request, cardSeq);
                   if ( bal.get("cardServiceDTO")!=null){
                       Object o = ((Map) bal.get("cardServiceDTO")).get("balance");
                       if (StringUtils.isNotBlank((String)o)){
                           Double  balance = Double.parseDouble(o.toString());
                           tatolBal = + balance;
                       }
                   }

               }
           }

            DecimalFormat df = new DecimalFormat("0.00");
            String balstr = df.format(tatolBal/100);

            tidings.setT(balstr);
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            e.printStackTrace();
        }

        return tidings;
    }











}
